﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace ImageP
{
    public partial class Fourier2 : Form
    {
        public Fourier2()
        {
            InitializeComponent();
        }
        FourierTransformation ft = new FourierTransformation();
        string filePath;
        ReadImage RI = new ReadImage();
        string fileExtension;
        Bitmap bm;
        string type;
        private void highpassFilterToolStripMenuItem_Click(object sender, EventArgs e)
        {

            type = "idealhigh";
            textBox1.Visible = true;
            label1.Visible = true;

            textBox2.Visible = false;
            label2.Visible = false;
        }

        private void lowpassFilterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            type = "ideallow";
            textBox1.Visible = true;
            label1.Visible = true;

            textBox2.Visible = false;
            label2.Visible = false;
        }

        private void lowpassFilterToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            type = "butterlow";
            textBox1.Visible = true;
            label1.Visible = true;

            textBox2.Visible = true;
            label2.Visible = true;
        }

        private void highpassFilterToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            type = "butterhigh";
            textBox1.Visible = true;
            label1.Visible = true;

            textBox2.Visible = true;
            label2.Visible = true;
        }

        private void lowpassFilterToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            type = "gaussianlow";
            textBox1.Visible = true;
            label1.Visible = true;

            textBox2.Visible = false;
            label2.Visible = false;
        }

        private void highpassFilterToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            type = "gaussianhigh";
            textBox1.Visible = true;
            label1.Visible = true;

            textBox2.Visible = false;
            label2.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (type == "idealhigh")
            {
                double x = double.Parse(textBox1.Text);
                Bitmap fn;
                IdealFilter iFilter = new IdealFilter();
                Bitmap bm = new Bitmap(pictureBox1.Image);
                fn = iFilter.HighpassIdeal(bm, x);
                pictureBox2.Image = fn;
            }
            else if (type == "ideallow")
            {
                double x = double.Parse(textBox1.Text);
                Bitmap fn;
                IdealFilter iFilter = new IdealFilter();
                Bitmap bm = new Bitmap(pictureBox1.Image);
                fn = iFilter.LowpassIdeal(bm, x);
                pictureBox2.Image = fn;
            }
            else if (type == "gaussianhigh")
            {
                GaussianFilter gaussian = new GaussianFilter();
                double D0 = double.Parse(textBox1.Text);
                Bitmap bm = new Bitmap(pictureBox1.Image);
                gaussian.get(bm);
                gaussian.ConstructFilterBuffer(D0, pictureBox1.Image.Width, pictureBox1.Image.Height, 2);
                gaussian.getRealGaussian();
                gaussian.getImaginaryGaussian();
                bm = gaussian.getInverse();

                pictureBox2.Image = bm;
            }
            else if (type == "gaussianlow")
            {
                GaussianFilter gaussian = new GaussianFilter();
                double D0 = double.Parse(textBox1.Text);
                Bitmap bm = new Bitmap(pictureBox1.Image);
                gaussian.get(bm);
                gaussian.ConstructFilterBuffer(D0, pictureBox1.Image.Width, pictureBox1.Image.Height, 1);
                gaussian.getRealGaussian();
                gaussian.getImaginaryGaussian();
                bm = gaussian.getInverse();

                pictureBox2.Image = bm;
            }
            else if (type == "butterlow")
            {
                ButterWorthFilter butterworth = new ButterWorthFilter();
                Bitmap bm = new Bitmap(pictureBox1.Image);
                Bitmap Fbm;
                Fbm = butterworth.lowpass(bm, int.Parse(textBox2.Text), double.Parse(textBox1.Text));
                pictureBox2.Image = Fbm;
            }
            else if (type == "butterhigh")
            {
                ButterWorthFilter butterworth = new ButterWorthFilter();
                Bitmap bm = new Bitmap(pictureBox1.Image);
                Bitmap Fbm;
                Fbm = butterworth.highpass(bm, int.Parse(textBox2.Text), double.Parse(textBox1.Text));
                pictureBox2.Image = Fbm;
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog getFile = new OpenFileDialog();
            getFile.ShowDialog();
            this.filePath = getFile.FileName;
            this.RI.FilePath = this.filePath;
            this.fileExtension = Path.GetExtension(this.filePath);

            if (this.fileExtension == ".jpg" || this.fileExtension == ".png" || this.fileExtension == ".gif" || this.fileExtension == ".bmp")
            {
                this.RI.LoadCommanFormats();
                Image im = Image.FromFile(this.filePath);
                pictureBox1.Width = im.Width;
                pictureBox1.Height = im.Height;
                pictureBox1.Image = im;
            }
            else
            {
                this.RI.LoadOtherFormats();
                this.bm = new Bitmap(this.RI.ImageWidth, this.RI.ImageHeight);
                pictureBox1.Width = this.RI.ImageWidth;
                pictureBox1.Height = this.RI.ImageHeight;
                this.bm = this.RI.OpenOtherFormats();
                pictureBox1.Image = this.bm;

            }
        }

        private void displayRedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Bitmap bm = new Bitmap(pictureBox1.Image);
            int[,] red;

            RI.Bitmap_Buffer(bm);
            ft.FrequencyDomain(bm, RI.ImageWidth, RI.ImageHeight);
            red = ft.getNormRed();
            RGBcolor[,] arr = new RGBcolor[RI.ImageHeight, RI.ImageWidth];
            for (int i = 0; i < RI.ImageHeight; i++)
            {
                for (int j = 0; j < RI.ImageWidth; j++)
                {
                    arr[i, j].Red = red[i, j];
                }
            }

            RI.Buffer = arr;
            bm = RI.OpenOtherFormats();
            pictureBox2.Image = bm;
        }

        private void displayBlueToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Bitmap bm = new Bitmap(pictureBox1.Image);
            int[,] blue;

            RI.Bitmap_Buffer(bm);
            ft.FrequencyDomain(bm, RI.ImageWidth, RI.ImageHeight);
            blue = ft.getNormBlue();
            RGBcolor[,] arr = new RGBcolor[RI.ImageHeight, RI.ImageWidth];
            for (int i = 0; i < RI.ImageHeight; i++)
            {
                for (int j = 0; j < RI.ImageWidth; j++)
                {
                    arr[i, j].Blue = blue[i, j];
                }
            }

            RI.Buffer = arr;
            bm = RI.OpenOtherFormats();
            pictureBox2.Image = bm;
        }

        private void displayGreenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Bitmap bm = new Bitmap(pictureBox1.Image);
            int[,] green;

            RI.Bitmap_Buffer(bm);
            ft.FrequencyDomain(bm, RI.ImageWidth, RI.ImageHeight);
            green = ft.getNormGreen();
            RGBcolor[,] arr = new RGBcolor[RI.ImageHeight, RI.ImageWidth];
            for (int i = 0; i < RI.ImageHeight; i++)
            {
                for (int j = 0; j < RI.ImageWidth; j++)
                {
                    arr[i, j].Green = green[i, j];
                }
            }

            RI.Buffer = arr;
            bm = RI.OpenOtherFormats();
            pictureBox2.Image = bm;
        }

        private void inverseFourierToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Bitmap bm = new Bitmap(pictureBox1.Image);
            Bitmap bmF = ft.inverseFreq(bm, pictureBox1.Image.Height, pictureBox1.Image.Width);
            pictureBox2.Image = bmF;
        }

       
    }
}
